<template>
      <body>
          <div>
              <img src="../assets/back.png" alt="返回" style="width:3rem;height:3rem;margin-bottom:2rem;cursor:pointer" @click="toHome"/>
          </div>
    <div class="container">
      <h1>登录</h1>
      <form>
        <div class="form-control">
          <input type="text" required v-model="username" autocomplete="off">
          <label>
              <span style="transition-delay: 0ms">用</span>
              <span style="transition-delay: 50ms">户</span>
              <span style="transition-delay: 150ms">名</span>
          </label>
        </div>
        <div class="form-control">
          <input type="password" required v-model="password" autocomplete="new-password">
          <label>
              <span style="transition-delay: 0ms">密</span>
              <span style="transition-delay: 50ms">码</span>
          </label>
        </div>

        <button class="btn" @click="login" type="button">登录</button>

        <p class="text">还没有账号？<a @click="toRegister" style="cursor:pointer">立即注册</a> </p>
      </form>
    </div>
  </body>
</template>
<script>
export default{
    name:'Login',
    component:{},
    data:function(){
        return{
            username:"",
            password:"",
        };
    },
    methods:{
        toRegister:function(){
            this.$router.push('/register');
        },
        toHome:function(){
            this.$router.push('/home');
        },
        login:function(){
          if(this.password.length==0||this.username.length==0){
            this.$message.warning('用户名或密码不能为空')
            return;
          }
          this.axios.post('/user/login',{username:this.username,password:this.password})
          .then(response=>{
            if(response.data.code===200){
              this.$message.success('登录成功！');
              this.$store.commit('login');
              this.$store.state.username=this.username;
              this.$store.state.password=this.password;
              this.$store.state.uid=response.data.data.id;
              document.cookie='uid'+'='+response.data.data.id;
              document.cookie="isLogin=true";
              document.cookie='username'+'='+this.username;
              document.cookie='password'+'='+this.password;
              console.log('read '+this.$store.state.uid+this.username);
              this.$router.push('/home');
            }else{
              this.$message.error(response.data.msg+'！登录失败！');
            }
          })
          .catch(error=>{
            console.log(error+'catch error');
            this.$message.error('登录失败！');
          })
          console.log("login");
        }
    },
}
</script>
<style scoped>
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
  box-sizing: border-box;
}

body {
  background-image: url("../assets/login.jpg");
  background-size: cover;
  color: #fff;
  font-family: 'Muli', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

.container {
  background-color: rgba(0, 0, 0, 0.4);
  padding: 20px 40px;
  border-radius: 5px;
}

.container h1 {
  text-align: center;
  margin-bottom: 30px;
}

.container a {
  text-decoration: none;
  color: lightblue;
}

.btn {
  cursor: pointer;
  display: inline-block;
  width: 100%;
  background: lightblue;
  padding: 15px;
  font-family: inherit;
  font-size: 16px;
  border: 0;
  border-radius: 5px;
}

.btn:focus {
  outline: 0;
}

.btn:active {
  transform: scale(0.98);
}

.text {
  margin-top: 30px;
}

.form-control {
  position: relative;
  margin: 20px 0 40px;
  width: 300px;
}

.form-control input {
  background-color: transparent;
  border: 0;
  border-bottom: 2px #fff solid;
  display: block;
  width: 100%;
  padding: 15px 0;
  font-size: 18px;
  color: #fff;
}

.form-control input:focus,
.form-control input:valid {
  outline: 0;
  border-bottom-color: lightblue;
}

.form-control label {
  position: absolute;
  top: 15px;
  left: 0;
  pointer-events: none;
}

.form-control label span {
  display: inline-block;
  font-size: 18px;
  min-width: 5px;
  transition: 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.form-control input:focus + label span,
.form-control input:valid + label span {
  color: lightblue;
  transform: translateY(-30px);
}
</style>
